package com.hxkcloud.nurse.activitys;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.beans.FileUploadBean;
import com.hxkcloud.nurse.beans.LoginInfo;
import com.hxkcloud.nurse.beans.resultbeans.ObjectResultBean;
import com.hxkcloud.nurse.config.UrlConfig;
import com.hxkcloud.nurse.dborm.LoginInfoDao;
import com.hxkcloud.nurse.popup.DateSelectorPopWindow;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.utils.StringUtil;
import com.hxkcloud.nurse.utils.Utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/10.
 */
public class CertificationActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private EditText nameET;
    private EditText workyearsET;
    private EditText titleET;
    private EditText schoolET;
    private EditText companyET;
    private EditText departmentET;
    private EditText picsET;

    private LinearLayout picsLayout;

    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    private ImageView iv4;
    private ImageView iv5;
    private ImageView iv6;
    private ImageView iv7;
    private ImageView iv8;
    private ImageView iv9;

    private boolean nameInput = false;
    private boolean workyeasInput = false;
    private boolean titleInput = false;
    private boolean schoolInput = false;
    private boolean companyInput = false;
    private boolean departmentInput = false;
    private boolean picsInput = false;

    private DateSelectorPopWindow dateSelect;

    private static final int DEL_PHOTO = 101;


    private Button saveBtn;

    private ObjectResultBean<String> resultBean;

    private List<FileUploadBean> fileResultBean;

    private ArrayList<String> imageUris;

    private LoginInfo submitInfo;

    String name;
    String age;
    String title = "";
    String department;
    String school;
    String company;

    private boolean isEdit = false;

    LoginInfoDao userDao;


    private String selectWorkYear = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout_certification);
        userDao = new LoginInfoDao(mContext);
        submitInfo = userDao.getUser();
        initView();
        initData();
        initListener();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_SUBMITCERTIFICATIONINFO_SUCCESS:
                dismisssLoading();
                if (msg.obj != null) {
                    resultBean = (ObjectResultBean<String>) msg.obj;

                    submitInfo.setUserId(loginInfo.getUserId());
                    submitInfo.setUserStyle(loginInfo.getUserStyle());
                    submitInfo.setName(name);
                    if(selectWorkYear.contains("-")){
                        submitInfo.setWorkingYears(Utils.date2TimeStamp(selectWorkYear, "yyyy-MM-dd"));
                    }else{
                        submitInfo.setWorkingYears(selectWorkYear);
                    }
                    submitInfo.setTitle(title);
                    submitInfo.setSchool(school);
                    submitInfo.setWorkUnit(company);
                    submitInfo.setDepartmentName(department);
                    System.out.println("submitInfo====" + submitInfo);
                    userDao.setUser(submitInfo);
                    toastMsg(resultBean.getMessage());
                    finish();

                }

                break;
            case MessageCode.NETWORKCODE_SUBMITCERTIFICATIONINFO_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                }

                break;

            case MessageCode.NETWORKCODE_FILEUPLOAD_SUCCESS:
                if (msg.obj != null) {
                    fileResultBean = (List<FileUploadBean>) msg.obj;
                    // 以http开头的都是已经上传好的图片，直接加入数组即可
                    List<String> uploadImages = new ArrayList<>();
                    for (String uri : imageUris) {
                        if (uri.startsWith("http")) {
                            uploadImages.add(uri);
                        }
                    }

                    for (int i = 0; i < fileResultBean.size(); i++) {
                        uploadImages.add(fileResultBean.get(i).getUrl());
                    }
                    submitInfo.setInformationImageList(uploadImages);

                    name = nameET.getText().toString();
                    age = workyearsET.getText().toString();
//                    title = loginInfo.getTitle();
                    department = departmentET.getText().toString();
                    school = schoolET.getText().toString();
                    company = companyET.getText().toString();

                    if(selectWorkYear.contains("-")){
                        YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(), name, department, title, Utils.date2TimeStamp(selectWorkYear, "yyyy-MM-dd"), school, company, uploadImages, handler);
                    }else{
                        YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(), name, department, title,selectWorkYear, school, company, uploadImages, handler);
                    }

                }
                break;
            case MessageCode.NETWORKCODE_FILEUPLOAD_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                    return;
                } else {
                    toastMsg(getResources().getString(R.string.fileuploadfail));
                }

        }

    }

    @Override
    protected void initView() {

        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.certification_text_title));
        iv_back.setOnClickListener(this);

        nameET = (EditText) findViewById(R.id.certification_et_name);
        workyearsET = (EditText) findViewById(R.id.certification_et_workyears);
        titleET = (EditText) findViewById(R.id.certfication_et_zhicheng);
        schoolET = (EditText) findViewById(R.id.certfication_et_school);
        companyET = (EditText) findViewById(R.id.certfication_et_company);
        departmentET = (EditText) findViewById(R.id.certfication_et_department);
        picsET = (EditText) findViewById(R.id.certfication_et_pics);

        picsLayout = (LinearLayout) findViewById(R.id.layout_cert_whole);

        iv1 = (ImageView) findViewById(R.id.iv_cert_img1);
        iv2 = (ImageView) findViewById(R.id.iv_cert_img2);
        iv3 = (ImageView) findViewById(R.id.iv_cert_img3);
        iv4 = (ImageView) findViewById(R.id.iv_cert_img4);
        iv5 = (ImageView) findViewById(R.id.iv_cert_img5);
        iv6 = (ImageView) findViewById(R.id.iv_cert_img6);
        iv7 = (ImageView) findViewById(R.id.iv_cert_img7);
        iv8 = (ImageView) findViewById(R.id.iv_cert_img8);
        iv9 = (ImageView) findViewById(R.id.iv_cert_img9);

        LinearLayout.LayoutParams lp_iv = new LinearLayout.LayoutParams((screenWidth - ImageUtil.dip2px(30, mContext)) / 3, screenWidth / 3);
        LinearLayout.LayoutParams lp_iv1 = new LinearLayout.LayoutParams((screenWidth - ImageUtil.dip2px(30, mContext)) / 3, screenWidth / 3);
        lp_iv1.leftMargin = ImageUtil.dip2px(5, mContext);
        iv1.setLayoutParams(lp_iv);
        iv2.setLayoutParams(lp_iv1);
        iv3.setLayoutParams(lp_iv1);
        iv4.setLayoutParams(lp_iv);
        iv5.setLayoutParams(lp_iv1);
        iv6.setLayoutParams(lp_iv1);
        iv7.setLayoutParams(lp_iv);
        iv8.setLayoutParams(lp_iv1);
        iv9.setLayoutParams(lp_iv1);

        nameET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    nameInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    nameInput = true;
                    if (workyeasInput && titleInput && schoolInput && companyInput && departmentInput && picsInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        schoolET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    schoolInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    schoolInput = true;
                    if (nameInput && workyeasInput && titleInput && companyInput && departmentInput && picsInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        companyET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    companyInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    companyInput = true;
                    if (nameInput && workyeasInput && titleInput && schoolInput && departmentInput && picsInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        picsET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (StringUtil.isEmpty(s.toString())) {
                    picsInput = false;
                    saveBtn.setBackgroundResource(R.drawable.bt_grey);
                    saveBtn.setEnabled(false);
                } else {
                    picsInput = true;
                    if (nameInput && workyeasInput && titleInput && schoolInput && companyInput && departmentInput) {
                        saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                        saveBtn.setEnabled(true);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        saveBtn = (Button) findViewById(R.id.certification_btn_save);
    }

    @Override
    protected void initListener() {
        iv_back.setOnClickListener(this);
        workyearsET.setOnClickListener(this);
        titleET.setOnClickListener(this);
        schoolET.setOnClickListener(this);
        companyET.setOnClickListener(this);
        departmentET.setOnClickListener(this);
        picsET.setOnClickListener(this);
        saveBtn.setOnClickListener(this);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
        iv4.setOnClickListener(this);
        iv5.setOnClickListener(this);
        iv6.setOnClickListener(this);
        iv7.setOnClickListener(this);
        iv8.setOnClickListener(this);
        iv9.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        imageUris = new ArrayList<>();

        System.out.println("loginInfo====" + loginInfo);

        if (loginInfo.getName() != null) {
            nameET.setText(loginInfo.getName());
            nameInput = true;
        }
        if (loginInfo.getWorkingYears() != null) {
            if ("".equals(loginInfo.getWorkingYears())) {
                workyearsET.setText("");
            } else if ("0".equals(loginInfo.getWorkingYears())) {
                workyearsET.setText("<1年");
            } else {
//                selectWorkYear = Utils.timeStamp2Date(loginInfo.getWorkingYears(),"yyyy-MM-dd");
//                int thisYear = (new Date()).getYear()+1900;
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                int workYear = 0;
//                try {
//                    workYear = sdf.parse(selectWorkYear).getYear()+1900;
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                int age = StringUtil.getAgeFromBirthday(new Date(Long.valueOf(loginInfo.getWorkingYears())));
                if (age == 0) {
                    workyearsET.setText("<1年");
                } else {
                    workyearsET.setText(age + "年");
                }
            }
            selectWorkYear = loginInfo.getWorkingYears();
            workyeasInput = true;

        }
        if (loginInfo.getTitle() != null) {
            title = loginInfo.getTitle();
            if (title.length() > 10) {
                String titletv = title.substring(0, 10) + "...";
                titleET.setText(titletv);
            } else {
                titleET.setText(title);
            }
            titleInput = true;
        }
        if (loginInfo.getSchool() != null) {
            schoolET.setText(loginInfo.getSchool());
            schoolInput = true;
        }
        if (loginInfo.getWorkUnit() != null) {
            companyET.setText(loginInfo.getWorkUnit());
            companyInput = true;
        }
        if (loginInfo.getDepartmentName() != null) {
            departmentET.setText(loginInfo.getDepartmentName());
            departmentInput = true;
        }
        if (loginInfo.getInformationImageList().size() > 0) {
            picsLayout.setVisibility(View.VISIBLE);
            picsInput = true;
            imageUris.addAll(loginInfo.getInformationImageList());
            for (int i = 0; i < loginInfo.getInformationImageList().size(); i++) {
                String imgStr = loginInfo.getInformationImageList().get(i);
                switch (i) {
                    case 0:
                        iv1.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv1, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            Log.i("--------->",i+":"+imageUris.get(i).toString());
                            imageLoader.displayImage(UrlConfig.image_host  +  imgStr, iv1, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 1:
                        iv2.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv2, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv2, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 2:
                        iv3.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv3, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(imgStr, iv3, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 3:
                        iv4.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv4, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv4, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 4:
                        iv5.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv5, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv5, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 5:
                        iv6.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv6, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv6, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 6:
                        iv7.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv7, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv7, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 7:
                        iv8.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv8, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv8, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                    case 8:
                        iv9.setVisibility(View.VISIBLE);
                        if (imgStr.startsWith("http")) {
                            imageLoader.displayImage(imgStr, iv9, ImageOptionUtils.getInstance().getSimplePicOptions());
                        } else {
                            imageUris.set(i, UrlConfig.image_host + imgStr);
                            imageLoader.displayImage(UrlConfig.image_host + imgStr, iv9, ImageOptionUtils.getInstance().getSimplePicOptions());
                        }
                        break;
                }
            }
//            if (getIndexIv(imageUris.size()) != null) {
//                getIndexIv(imageUris.size()).setVisibility(View.VISIBLE);
//            }
        } else {
            picsLayout.setVisibility(View.GONE);
            picsInput = false;
        }

        if (nameInput && workyeasInput && titleInput && schoolInput && companyInput && departmentInput && picsInput) {
            saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
            saveBtn.setEnabled(true);
        }

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_cert_img1:
                seePhotos(0);
                break;
            case R.id.iv_cert_img2:
                seePhotos(1);
                break;
            case R.id.iv_cert_img3:
                seePhotos(2);
                break;
            case R.id.iv_cert_img4:
                seePhotos(3);
                break;
            case R.id.iv_cert_img5:
                seePhotos(4);
                break;
            case R.id.iv_cert_img6:
                seePhotos(5);
                break;
            case R.id.iv_cert_img7:
                seePhotos(6);
                break;
            case R.id.iv_cert_img8:
                seePhotos(7);
                break;
            case R.id.iv_cert_img9:
                seePhotos(8);
                break;
            case R.id.iv_title_back://back
                finish();
                break;
            case R.id.certfication_et_pics:
                Intent intent = new Intent(CertificationActivity.this, CertificationPicsActivity.class);
                intent.putStringArrayListExtra("imageUris", imageUris);
                startActivityForResult(intent, 1);
                break;
            case R.id.certification_et_workyears:
                closeInputMethodManager();
                if (dateSelect == null) {
                    dateSelect = new DateSelectorPopWindow(this);
                    dateSelect.setTimeListener(new DateSelectorPopWindow.TimeListener() {
                        @Override
                        public void setTime(String time) {
                            String timeStr = "";
                            if (time.contains(".")) {
                                timeStr = time.replace(".", "-");
                            }
                            selectWorkYear = timeStr;
                            workyeasInput = true;
                            int thisYear = (new Date()).getYear() + 1900;
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            int workYear = 0;
                            try {
                                workYear = sdf.parse(timeStr).getYear() + 1900;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            int years = getWorkyears(thisYear, workYear);
                            if (years == 0) {
                                workyearsET.setText("<1年");
                            } else {
                                workyearsET.setText(years + "年");
                            }


                        }
                    });
                }
                dateSelect.show(ll_title);
                break;
            case R.id.certfication_et_zhicheng:
                Intent titleIntent = new Intent(CertificationActivity.this, TitleForDNActivity.class);
                titleIntent.putExtra("name", title);
                startActivityForResult(titleIntent, 1);
                break;
            case R.id.certfication_et_department:
                Intent departmentIntent = new Intent(this, DepartmentForDNActivity.class);
                departmentIntent.putExtra("type", 2);
                departmentIntent.putExtra("name", departmentET.getText().toString().trim());
                startActivityForResult(departmentIntent, 2);
                break;
            case R.id.certfication_et_school:
                Intent schoolIntent = new Intent(this, DepartmentForDNActivity.class);
                schoolIntent.putExtra("type", 3);
                schoolIntent.putExtra("name", schoolET.getText().toString().trim());
                startActivityForResult(schoolIntent, 3);
                break;
            case R.id.certfication_et_company:
                Intent companyIntent = new Intent(this, DepartmentForDNActivity.class);
                companyIntent.putExtra("type", 4);
                companyIntent.putExtra("name", companyET.getText().toString().trim());
                startActivityForResult(companyIntent, 4);
                break;
            case R.id.certification_btn_save:

//              YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(),name,department,title,Utils.date2TimeStamp(workyearsET.getText().toString(),"yyyy-MM-dd"),school,company,"",handler);

//                List<File> images = new ArrayList<>();
//                for (String img : imageUris) {
//                    images.add(new File(img));
//                }
//                YiJiaHuNetWorkUtils.FileUpload(images, YjhApplication.file_image, handler);

                loading();
                if (imageUris != null && imageUris.size() > 0) {
                    if (imageUris.size() > 0) {
                        List<File> images = new ArrayList<>();//记录新加的图片
                        for (String imageUri : imageUris) {
                            if (!imageUri.contains("image")) {
                                images.add(new File(imageUri));

                            }
                        }
                        if (images.size() > 0) {

                            YiJiaHuNetWorkUtils.FileUpload(images, YjhApplication.file_image, handler);
                        } else {
                            List<String> oldImages = new ArrayList<>();
                            for (String imageUri : imageUris) {
                                oldImages.add(imageUri);
                            }
                            name = nameET.getText().toString();
                            age = workyearsET.getText().toString();
//                            title = loginInfo.getTitle();
                            department = departmentET.getText().toString();
                            school = schoolET.getText().toString();
                            company = companyET.getText().toString();
                            submitInfo.setInformationImageList(oldImages);
                            if(selectWorkYear.contains("-")){
                                YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(), name, department, title, Utils.date2TimeStamp(selectWorkYear, "yyyy-MM-dd"), school, company, oldImages, handler);
                            }else{
                                YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(), name, department, title,selectWorkYear, school, company, oldImages, handler);
                            }
                        }
                    } else {
                        List<File> images = new ArrayList<>();
                        for (String img : imageUris) {
                            images.add(new File(img));
                        }
                        YiJiaHuNetWorkUtils.FileUpload(images, YjhApplication.file_image, handler);
                    }
                } else {
                    List<String> oldImages = new ArrayList<>();
                    if (imageUris.size() > 0) {
                        for (String imageUri : imageUris) {
                            oldImages.add(imageUri);
                        }
                    }
                    name = nameET.getText().toString();
                    age = workyearsET.getText().toString();
                    title = titleET.getText().toString();
                    department = departmentET.getText().toString();
                    school = schoolET.getText().toString();
                    company = companyET.getText().toString();
                    submitInfo.setInformationImageList(oldImages);
                    if(selectWorkYear.contains("-")){
                        YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(), name, department, title, Utils.date2TimeStamp(selectWorkYear, "yyyy-MM-dd"), school, company, oldImages, handler);
                    }else{
                        YiJiaHuNetWorkUtils.submitCertificationInfo(loginInfo.getUserId(), name, department, title,selectWorkYear, school, company, oldImages, handler);
                    }


                }

                break;
        }
    }

    public int getWorkyears(int thisyear, int workyear) {
        if (thisyear == workyear) {
            return 0;
        } else {
            return thisyear - workyear;
        }

    }

    private ImageView getIndexIv(int index) {
        ImageView iv = null;
        switch (index) {
            case 0:
                iv = iv1;
                break;
            case 1:
                iv = iv2;
                break;
            case 2:
                iv = iv3;
                break;
            case 3:
                iv = iv4;
                break;
            case 4:
                iv = iv5;
                break;
            case 5:
                iv = iv6;
                break;
            case 6:
                iv = iv7;
                break;
            case 7:
                iv = iv8;
                break;
            case 8:
                iv = iv9;
                break;
        }
        return iv;
    }

    private void seePhotos(int index) {
        Intent iPhotos = new Intent(mContext, PhotosActivity.class);
        iPhotos.putExtra("index", index);
        iPhotos.putExtra("canDel", false);
        iPhotos.putStringArrayListExtra("urls", imageUris);
        startActivity(iPhotos);
//        startActivityForResult(iPhotos, DEL_PHOTO);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case RESULT_OK:
                if (data != null) {
                    if (requestCode == 2) {
                        String departmentS = data.getStringExtra("name");
                        departmentET.setText(departmentS);
                        departmentInput = true;
                    } else if (requestCode == 3) {
                        String schoolS = data.getStringExtra("name");
                        schoolET.setText(schoolS);
                        schoolInput = true;
                    } else if (requestCode == 4) {
                        String companyS = data.getStringExtra("name");
                        companyET.setText(companyS);
                        companyInput = true;
                    }
                }
                break;
            case 1:
                if (data != null) {
                    String titleresult = data.getStringExtra("title");
                    title = titleresult;
                    if (title.length() > 10) {
                        String titletv = title.substring(0, 10) + "...";
                        titleET.setText(titletv);
                    } else {
                        titleET.setText(title);
                    }
                    titleInput = true;
                }

                break;
            case 2:
                if (data != null) {
                    String departmentresult = data.getStringExtra("department");
                    departmentET.setText(departmentresult);
                    departmentInput = true;
                }
                break;
            case 3:
                if (data != null) {
                    imageUris.clear();
                    iv1.setVisibility(View.GONE);
                    iv2.setVisibility(View.GONE);
                    iv3.setVisibility(View.GONE);
                    iv4.setVisibility(View.GONE);
                    iv5.setVisibility(View.GONE);
                    iv6.setVisibility(View.GONE);
                    iv7.setVisibility(View.GONE);
                    iv8.setVisibility(View.GONE);
                    iv9.setVisibility(View.GONE);
                    imageUris = data.getStringArrayListExtra("pics");
                    System.out.println("imageUris====" + imageUris);
                    if (imageUris != null && imageUris.size() > 0) {
                        picsET.setText("");
                        picsInput = true;
                        if (nameInput && workyeasInput && titleInput && schoolInput && departmentInput && workyeasInput) {
                            saveBtn.setBackgroundResource(R.drawable.bg_btn_lightgreen_corner);
                            saveBtn.setEnabled(true);
                        }
                        picsLayout.setVisibility(View.VISIBLE);
                        for (int i = 0; i < imageUris.size(); i++) {
                            switch (i) {
                                case 0:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv1, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv1.setImageBitmap(mBitmap0);
                                    }
                                    iv1.setVisibility(View.VISIBLE);
                                    break;
                                case 1:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv2, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv2.setImageBitmap(mBitmap0);
                                    }
                                    iv2.setVisibility(View.VISIBLE);
                                    break;
                                case 2:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv3, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv3.setImageBitmap(mBitmap0);
                                    }
                                    iv3.setVisibility(View.VISIBLE);
                                    break;
                                case 3:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv4, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv4.setImageBitmap(mBitmap0);
                                    }
                                    iv4.setVisibility(View.VISIBLE);
                                    break;
                                case 4:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv5, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv5.setImageBitmap(mBitmap0);
                                    }
                                    iv5.setVisibility(View.VISIBLE);
                                    break;
                                case 5:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv6, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv6.setImageBitmap(mBitmap0);
                                    }
                                    iv6.setVisibility(View.VISIBLE);
                                    break;
                                case 6:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv7, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv7.setImageBitmap(mBitmap0);
                                    }
                                    iv7.setVisibility(View.VISIBLE);
                                    break;
                                case 7:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv8, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv8.setImageBitmap(mBitmap0);
                                    }
                                    iv8.setVisibility(View.VISIBLE);
                                    break;
                                case 8:
                                    if (imageUris.get(i).startsWith("http")) {
                                        imageLoader.displayImage(imageUris.get(i), iv9, ImageOptionUtils.getInstance().getSimplePicOptions());
                                    } else {
                                        Bitmap mBitmap0 = BitmapFactory.decodeFile(imageUris.get(i));
                                        iv9.setImageBitmap(mBitmap0);
                                    }
                                    iv9.setVisibility(View.VISIBLE);
                                    break;

                            }

                        }
                    } else {
                        picsLayout.setVisibility(View.GONE);
                        picsInput = false;
                    }
                }


        }
    }
}
